<?php

class AppConfiguration {
    //path setting.
    const REPORT_TOOL_CONFIG_FILE = '/data/mytvplus/mstat_report_tool/conf/mstat_report_tool_actions.xml';
    const REPORT_TOOL_DB_CONFIG = '/data/mytvplus/mstat_report_tool/conf/mstat_report_tool_db.xml';
    const REPORT_TOOL_PAGES_FILE = '/data/mytvplus/mstat_report_tool/conf/pages.xml';
    const REPORT_TOOL_MEMCACHE_FILE = '/data/mytvplus/mstat_report_tool/conf/mstat_report_tool_memcache.xml';
    const REPORT_TOOL_SETTING_FILE = '/data/mytvplus/mstat_report_tool/conf/mstat_report_tool_setting.xml';
    const REPORT_TOOL_BROWSER_VIEW_FILE = '/data/mytvplus/mstat_report_tool/conf/mstat_report_tool_browser.xml';
    const REPORT_TOOL_OS_VIEW_FILE = '/data/mytvplus/mstat_report_tool/conf/mstat_report_tool_os.xml';
    const REPORT_TOOL_HOTSEARCH_CONFIG_FILE = '/data/mytvplus/mstat_report_tool/conf/mstat_report_tool_hotsearch_db.xml';
    //company code default
	const DEFAULT_COMPANY_CODE = 'TVB';
    
    //set the base lib path.
    const DEFAULT_BASE_LIB_PATH = '/opt/php/lib/php/TVB/mytvplus/MstatReportTool';
    const DEFAULT_SHORT_LIB_PATH = 'TVB/mytvplus/MstatReportTool';    

    //set page info.
    const CDN_PATH = '/data/httpd/htdocs/mytvplus.tvb.com/mstat.mytvplus.tvb.com/cdn/';
    const APP_TPL_ROOT = '/view/tpl/';
    
    //set export temp file path
    const EXPORT_TMP_FILE_PATH = 'view/data';
    const EXPORT_TMP_FILE_NAME = 'tmp_mstat_export_';
    const EXPORT_FILE_PREFIX_NAME_VIDEO_VIEWING = 'permuim_video_report';
    const EXPORT_FILE_PREFIX_NAME_SIGN_UP = 'sign-up_diagnostic_report';
    const EXPORT_FILE_PREFIX_NAME_PLAN = 'plan_subscription_report';
    const EXPORT_FILE_PREFIX_NAME_VIDEO_BITRATE = 'video_bitrate_report';
    const EXPORT_FILE_PREFIX_NAME_VIDEO_OS_BROWSER = 'OS_browser_reprot';
    const EXPORT_FILE_PREFIX_NAME_DAILY_SUMMARY = 'daily_summary_total';
    const EXPORT_FILE_PREFIX_NAME_REG_ACTIVE_CONFRIM = 'reg_active_confirm';
    const EXPORT_FILE_PREFIX_NAME_LOGIN_AND_LOGOUT = 'login_and_logout';
    const EXPORT_FILE_PREFIX_NAME_FORGET_PASSWORD = 'forget_password';
    const EXPORT_FILE_PREFIX_NAME_EDIT_PROFILE = 'edit_profile';
    const EXPORT_FILE_PREFIX_NAME_MEMBER_STAT = 'member_stat';
    const EXPORT_FILE_PREFIX_NAME_HOT_SEARCH = 'hot_search';
    

    //set the APC key.
    const APC_KEY_OF_PLAN_PACKAGE_XML_LIST = 'plan_package_xml_f';
    const APC_KEY_OF_CATEGORYS_XML_LIST = 'categorys_xml_f';

    //logic static var.
    const VIDEO_VIEWING_REPORT_GENDER_AGE_GROUP = 'Gender & Age Group';
    const VIDEO_VIEWING_REPORT_GENDER_GROUP = 'Gender Group';
    const VIDEO_VIEWING_REPORT_REGION_COUNTRY_GROUP = 'Region & Country Group';
    const VIDEO_VIEWING_REPORT_SUBSCRIPTION_PLAN = 'Subscription Plan Group';
    const VIDEO_VIEWING_REPORT_TOTAL_SUMMARY = 'Total Summary';
    const SUBSCRIPTION_PLAN_TABLE_HEADER_NEW = 'New';
    const SUBSCRIPTION_PLAN_TABLE_HEADER_RENEW = 'Renew';
    const SIGN_UP_DIAGNOSTIC_TITLE_OF_REGION_COUNTRY = 'Region/Country';
    const SUBSCRIPTION_PLAN_TYPE_OF_AGE = 'AGEGROUP';
    const SUBSCRIPTION_PLAN_TYPE_OF_COUNTRY = 'COUNTRY';
    const DAILY_SUMMARY_REPORT = 'Daily Automated Executive Summary Report';
    const DAILY_SUMMARY_VIEW_FREQUENCY = 'View Frequency';
    const DAILY_SUMMARY_TOTAL_BITRATE = 'Total Streaming Duration with bitrate';
    const DAILY_SUMMARY_UNIQUE_USER = 'Number of unique user';
    const DAILY_SUMMARY_SUBSCRIPTION = 'Subscriber with package';
    const DAILY_SUMMARY_ERRORS_REPORT = 'Number of Errors reported';
    const MEMBERSHIP_REG_ACTIVE_CONFIRM_TITLE = 'Reg/Active/Confirm Report';
    const MEMBERSHIP_LOGIN_LOGOUT_TITLE = 'Login And Logout Report';
    const MEMBERSHIP_FORGET_PASSWORD_TITLE = 'Forget Password Report';
    const MEMBERSHIP_EDIT_PROFILE_TITLE = 'Edit Profile Report ';
    
       
    //table name setting.
    const HOT_SEARCH_PRODUCT_NAME = 'product';

    //set the page navigation.
    static public $mod_topnav = array(
                                        array(
                                                'title'=>'Membership',
                                                'children'=>array(
                                                                array('title'=>'Reg/Active/Confirm Report','url'=>'reg_active_confirm.html'),
                                                                array('title'=>'Login And Logout Report','url'=>'mstat_login_out.html'),
                                                                array('title'=>'Forget Password Report','url'=>'mstat_forget_password.html'),
                                                                array('title'=>'Edit Profile Report','url'=>'mstat_edit_profile.html'),
                                                                array('title'=>'Members Stat Report','url'=>'mstat_member.html'),
                                                            ),

                                        ),
                                        array(
                                                'title'=>'myTV+',
                                                'children'=>array (
                                                                array('title'=>'Premium Video Viewing Report','url'=>'premium_video_viewing.html'),
                                                                array('title'=>'Video Bitrate Report','url'=>'video_bitrate.html'),
                                                                array('title'=>'Plan Subscription Report','url'=>'plan_subscription.html'),
                                                                array('title'=>'Sign-up Diagnostic Report','url'=>'sign_up.html'),
                                                                array('title'=>'OS And Browser Report','url'=>'os_browser.html'),
                                                                array('title'=>'Daily Automated Executive Summary Report','url'=>'daily_summary.html'),
                                                            ),
                                        ),
                                        array(
                                                'title'=>'Hot Search',
                                                'children'=>array(
                                                                array('title'=>'Hot Search Keywords Report','url'=>'hot_search.html'),
                                                            ),
                                        ),
                                );

    static public $member_showby_list = array('month' => 'By Month','date' => 'By Date','hour' => 'By Hour',);
    static public $member_showby_list_tmp1 = array('month'=>'By Month','date'=>'By Date');
	static public $member_first_login_options = array('0'=>'ALL','1'=>'First Login Edit Profile',);

    static public $daily_format_type = array('login'=>1,
                                             'logout'=>5,
                                             'active'=>3,
                                             'confirm'=>2,
                                             'register'=>0,
                                             'expired'=>6,
                                             'expired_confirm'=>4,
                                            );

    //service path setting.
    static public $country_service = 'get_country.html';
    static public $service_get_country = 'get_country.html';

    //video sort setting.
    static public $frequency_duration_sortby = array('frequency' => 'Frequency','duration' => 'Duration');
   
    //gender select list.
    static public $gender_list = array('0' => 'NONE','1' => 'ALL','2' => 'Male','3' => 'Female');
    static public $gender_array = array('2'=>'M','3'=>'F');
    
    //default dateby list.
    static public $dateby_list = array('month' => 'By Month','date' => 'By Date');
    
    //default date month sort by list.
    static public $date_month_sortby = array('date' => 'Date','month' => 'Month');
    
    //
    static public $category_filter_list = array(
                                                '1'=>'drama',
                                                '7'=>'variety',
                                                '19'=>'foodandtravel',
                                                '23'=>'lifestyle',
                                                '30'=>'news',
                                                '35'=>'music',
                                                '42'=>'kids',
                                                '46'=>'info',
                                                '47'=>'others');

    //default alias table name.
    static public $table_alias_name = 'Tbl_0';

    //default bitrate list
    static public $video_default_none = array('NONE'=>0);
    
    //subscription's plan
    static public $video_subscription_plan = array('free' => 'Free','premium' => 'Premium',);
    static public $video_subscription_alias = array('1'=>'Free','2'=>'Premium');

    static public $video_bitrates = 'bitrate';
    static public $video_plan_package = 'plan_package';
    static public $category_list = 'categorys';
    static public $age_list = 'age_group';
    static public $browsers = 'browsers';
    static public $os = 'OS';
    
    static public $sign_up_page_types = array('free'=>1,'facebook'=>2,'premium'=>3);
    static public $sign_up_report_title = array('free'=>'Free Membership','facebook'=>'Facebook','premium'=>'Premium');
    static public $sign_up_report_format = array(
                                                    'free'=>array(
                                                                '1'=>'FM_S1',
                                                                'a'=>'FM_A',
                                                            ),
                                                    'facebook'=>array(
                                                                '1'=>'FB_S1',
                                                                '2'=>'FB_S2',
                                                                'a'=>'FB_A',
                                                            ),
                                                    'premium'=>array(
                                                                '1'=>'P_S1',
                                                                '2'=>'P_S2',
                                                                '3'=>'P_S3',
                                                                '4'=>'P_S4',
                                                                'a'=>'P_A',
                                                            ),
                                                );
    static public $sign_up_remark_list = array(
                                                'FM_S1'=>'Free Membership Registration - Fill In The Form',
                                                'FM_A'=>'Free Membership - Activate Complete',
                                                'FB_S1'=>'Facebook - Fill In The Form',
                                                'FB_S2'=>'Facebook - Personal Details',
                                                'FB_A'=>'Facebook - Activate Complete',
                                                'P_S1'=>'Premium - Select Plan',
                                                'P_S2'=>'Premium - Personal Details',
                                                'P_S3'=>'Premium - Payment',
                                                'P_S4'=>'Premium - Confirmation',
                                                'P_A'=>'Activate Complete',
                                         );


    static public $top_size = array('10'=>'10', '20'=>'20', '30'=>'30', '40'=>'40', '50'=>'50');
    static public $top_size_default_value = 10;
	static public $country_stat = array(
                                            'Africa'=> 'Africa',
                                            'Asia'=> 'Asia',
                                            'Europe'=> 'Europe',
                                            'North America'=> 'North America',
                                            'South America'=> 'South America',
                                            'Oceania'=> 'Oceania',
                                            'Others'=> 'Others',
	                                    );


    static public $jq_cfg = array(
                                    'url'=>'services/get_daily_archive.php',
                                    'colNames'=>array('Date','Email','Action'),
                                    'colModel'=>array(
                                        array('name'=>'date','index'=>'date','width'=>'150','align'=>'center'),
                                        array('name'=>'name','index'=>'name','width'=>'370'),
                                        array('name'=>'action','index'=>'action','width'=>'100','align'=>'center','sortable'=>false)
                                    ),
                                    'rowNum'=>'10',
                                    'rowList'=>array(5,10,20,50)
                                );

    static public $jq_email = array(
                                    'url'=>'daily_emails.html?opt=get',
                                    'colNames'=>array('ID','Name','Email'),
                                    'colModel'=>array(
                                        array('name'=>'id','index'=>'id','width'=>'50','align'=>'center'),
                                        array('name'=>'name','index'=>'name','width'=>'300','align'=>'center','editable'=>true),
                                        array('name'=>'email','index'=>'email','width'=>'570','align'=>'center','editable'=>true,'email'=>true),
                                    ),
                                    'rowNum'=>'5',
                                    'rowList'=>array(5,10,20,50),
                                    'editurl'=> 'daily_emails.html?opt=edits',
                                );
    
    static public $default_bitrate_null = array(
                                            array('bitrate'=>'200','Total'=>0),
                                            array('bitrate'=>'500','Total'=>0),
                                            array('bitrate'=>'1500','Total'=>0),
                                           );


	static public $member_product_list = array('0'=> 'ALL',);
    static public $show_top_limit_hotsearch_keyword = array('50'=>'50','100'=>'100','200'=>'200');
	static public $member_bycountry_list = array(
                                                '0'=>'No',
                                                '1'=>'Yes',
	                                        );

    //set the title of membership item when show in result pages.
    static public $title_of_membership_reg_active_confirm = array('注冊人數','確認人數','激活人數','過期人數(未確認)','過期人數(已確認)');
    static public $title_of_membership_login_out = array('登入人數','登出人數');
    static public $title_of_membership_forget_password = array('人數');
    static public $title_of_membership_editprofile = array('人數');

    //public methods.
    static public function getInstance() {
        static $instance = null ;
        return $instance ? ($instance) : ($instance = new AppConfiguration()) ;
    }

    //default data setting for pages.
    static public function getDataByList() {
        $today = date('Y-m-d');
        $seven_days_before = date('Y-m-d',strtotime($today)-6*24*3600);
        $to_day = date('Y-m-d');
        $today_year = date('Y');
        $top_size_default = self::$top_size_default_value;
        $dateby_list = array('start_end_day'=>array($seven_days_before,$to_day),
                             'country'=>null,
                             'age'=>0,
                             'categorys'=>array('0'),
                             'gender'=>0,
                             'video_sortby'=>'frequency',
                             'video_subscription_plan'=> 'free',
                             'video_plan_package'=> array(0),
                             'dateby'=> 'month',
                             'custom_country'=>array('Asia'=>array('Asia'=>array('CN', 'HK', 'TW'))),
                             'bymonth2'=> '<input type="hidden" class="datebymonth" month="'.date("m").'" year="'.date("Y").'" />',
                             'bymonth'=> '<input type="hidden" class="datebymonth" month="'.date("m").'" year="'.date("Y").'" />',
                             'date_month_sortby'=>'month',
                             'top_size'=>$top_size_default,
                             'daily_date'=> $today,
                             'member_showbydate'=>'<input type="hidden" class="datebymonth" day="'.date("d").'" month="'.date("m").'" year="'.date("Y").'" />',
                             'daily_date'=> $today,
                             'member_dateby'=> 'month',
                             'byyear'=> $today_year,
                             'member_top_limit'=>100,
                             'member_product'=>'all',
                        );
        return $dateby_list;
    }

    //get the country list by Area.
    static protected function getCountryList($area) {
        $result = getStatsClass::getInstance()->getStatsByKey($area);
        return $result;
    }

    //static function.
    static public function getSignUpDiagnosticTableCol() {
        $main_title_of_table = self::$sign_up_report_title;
        $sub_title_of_table = self::$sign_up_report_format;
        $total = 0;
        foreach($main_title_of_table as $k1=>$v1) {
            foreach($sub_title_of_table[$k1] as $k2=>$v2) {
                $total+=1;
            }
        }
        return $total;
    }

    //setting the tools bar.
    static public function exportHtmlControl($rand_code,$type) {
        return '<a href="export.html?randcode='.$rand_code.'&type='.$type.'&style=html" target="_blank">HTML</a>';
    }
    static public function exportPdfControl($rand_code,$type) {
        return null;
        return '<a href="#" target="_blank">PDF</a> , ';
    }
    static public function exportCvsControl($rand_code,$type) {
        return null;
        //return '<a href="export.html?randcode='.$rand_code.'&type='.$type.'&style=csv" target="_blank">CSV</a>';
        //return 'CSV';
    }
    
    //get the team of browser when need to show.
    static public function getBrowserTeamForView() {
        $config = ConfigReaderFactory::getInstance()->getConfigReader(self::REPORT_TOOL_BROWSER_VIEW_FILE)->getConfig();
        $list = array();
        foreach($config->groupby as $key=>$value) {
            foreach($value->display as $b_k=>$b_v) {
                $name = (string)$b_v->attributes()->name;
                $pattern = array();
                foreach($b_v->browser as $p_k=>$p_v) {
                    $pattern[] = (string)$p_v->attributes()->name;
                }
                $list[$name] = $pattern;
            }
        }
        return $list;
    }

    //get the team of OS when need to show.
    static public function getOSTeamForView() {
        $config = ConfigReaderFactory::getInstance()->getConfigReader(self::REPORT_TOOL_OS_VIEW_FILE)->getConfig();
        $list = array();
        foreach($config->groupby as $key=>$value) {
            foreach($value->display as $b_k=>$b_v) {
                $name = (string)$b_v->attributes()->name;
                $pattern = array();
                foreach($b_v->os as $p_k=>$p_v) {
                    $pattern[] = (string)$p_v->attributes()->name;
                }
                $list[$name] = $pattern;
            }
        }
        return $list;
    }

    //get the new tearm of filter page.
    static public function getOSTearmOfFilter() {
        $config = ConfigReaderFactory::getInstance()->getConfigReader(self::REPORT_TOOL_OS_VIEW_FILE)->getConfig();
        $list = array();
        foreach($config->groupby as $key=>$value) {
            foreach($value->display as $b_k=>$b_v) {
                $name =(string)$b_v->attributes()->name;
                $list[$name] = $name;
            }
        }
        return $list;
    }

    //get the new tearm of filter page.
    static public function getBrowserTearmOfFilter() {
        $config = ConfigReaderFactory::getInstance()->getConfigReader(self::REPORT_TOOL_BROWSER_VIEW_FILE)->getConfig();
        $list = array();
        foreach($config->groupby as $key=>$value) {
            foreach($value->display as $b_k=>$b_v) {
                $name = (string)$b_v->attributes()->name;
                $list[$name] = $name;
            }
        }
        return $list;
    }

    //filter & merge the browser or OS.
    static public function filterAndMergeBrowser($browser_name) {
        $browser_pattern_list = self::getBrowserTeamForView();
        $browser = null;
        foreach($browser_pattern_list as $name_k=>$pattern_v) {
            foreach($pattern_v as $p_k=>$p_list) {
                if (eregi($p_list,$browser_name)==false) continue ;
                $browser = $name_k ;
                break ;
             }
        }
        $browser = (empty($browser)) ? 'Other' : $browser;
        return trim($browser);
    }

    //merge OS list.
    static public function filterAndMergeOs($os_name) {
        $os_pattern_list = self::getOSTeamForView();
        $os = null;
        foreach($os_pattern_list as $name_k=>$pattern_v) {
            foreach($pattern_v as $p_k=>$p_list) {
                if (eregi($p_list,$os_name)==false) continue ;
                $os = $name_k ;
                break ;
             }
        }
        $os = (empty($os)) ? 'Other' : $os;
        return trim($os);
    }



    //get the some config value.
    public function getConfigSetting($title) {
        if ($title==self::$video_plan_package) return $this->getPlanPackages();
        if ($title==self::$category_list) return $this->getCategorys();
        $result = array();
        $config = ConfigReaderFactory::getInstance()->getConfigReader(self::REPORT_TOOL_SETTING_FILE)->getConfig();
        foreach($config as $key=>$value) {
            if ($key!=$title) continue;
            $tmp = array();
            foreach($value as $k=>$v) {
                $data = (array)$v;
                $key_ = $data['@attributes']['value'];
                $val_ = $data[0];
                $tmp[$key_] = $val_;
            }
            $result[$key] = $tmp;
        }
        return $result[$title];
    }
    
    public function getPlanPackages() {
        $tmp_data = $this->getApcData(self::APC_KEY_OF_PLAN_PACKAGE_XML_LIST);
        if (empty($tmp_data)) {
            include_once "TVB/mytvplus/portal_service/client/requires.php";
            $client = new PortalWebServiceClient(1,1,1);
            $result = $client->get('package.list');
            if (!isset($result['data']) || empty($result['data'])) return array('0'=>'All');
            $data = array();
            foreach((array)$result['data'] as $list) {
                $d_key_v = (!empty($list['title'])) ? $list['title'] : $list['description'];
                if (trim($d_key_v)=='') continue;
                $package_id = $list['package_id'];
                $version_id = $list['version_id'];
                $data[$package_id][$version_id] = $d_key_v;
            }

            $tmp_data = array('0'=>'All');
            foreach($data as $package_id=>$package_info) {
                
                if (count($package_info)>1) {
                    asort($package_info);
                    $tmp_arr = array_reverse($package_info);
                    unset($package_info);
                    $package_info = $tmp_arr;
                }
                $tmp_data[$package_id] = array_shift($package_info);
            }
            apc_store(self::APC_KEY_OF_PLAN_PACKAGE_XML_LIST,$tmp_data);
        }
        return $tmp_data;
    }

    public function getCategorys() {
        $data = $this->getApcData(self::APC_KEY_OF_CATEGORYS_XML_LIST);
        if (empty($data)) {
            include_once 'TVB/PMSWebService/Client/requires.php';
            $client = new PMSWebServiceClient();
            $result = $client->getCategorys();
            if (empty($result) || $result==-1) return array('0'=>'NONE','all'=>'ALL');
            $filter_list = self::$category_filter_list;
            $category_infos = $client->getCategoryInformationByCategoryIds($result);
            $data = array('0'=>'None','all'=>'All');
            foreach($category_infos as $key=>$value) {
                if ($value['parent_id']==0 && in_array($value['id'],array_keys($filter_list))) {
                    $c_key = $value['path'];
                    $c_val = $value['name_english'];
                    $data[$c_key] = $c_val;
                }
            }
            if (!empty($data)) {
                apc_store(self::APC_KEY_OF_CATEGORYS_XML_LIST,$data);
            }
        }
        return (!empty($data)) ? $data : array();
    }
    
    public function getHotSearchProduct() {
        $default_list = array('all'=>'All');
        $reader = DBConnPoolFactory::getInstance()->getConnPool(self::REPORT_TOOL_HOTSEARCH_CONFIG_FILE)->getReadConn();
        $query = "SELECT id,name FROM ".self::HOT_SEARCH_PRODUCT_NAME." ORDER BY id ASC ";
        $ps = $reader;
        $ps = $reader->prepare($query);
        $ret = $ps->execute();
        if (!$ret) {
            error_log("File '".__FILE__."', "."Line ".__LINE__.": ".print_r($ps->errorInfo(), 1));
            return $default_list;
        }
        if ($ps->rowCount()>0) {
            $result = $ps->fetchAll(PDO::FETCH_ASSOC);
            foreach($result as $key=>$val) {
                $key_ = $val['name'];
                $default_list[$key_] = $key_;
            }
        }
        return $default_list;
    }

    //get country list for header string.
    public function getCountryListToHeader($country_arr) {
        $region_country_list = 'NONE';
        $tmp = array();
        foreach($country_arr as $k1=>$v1) {
            $tmp_stat = '<b>'.$k1.'</b> - ';
            $tmp_section = array();
            if (!is_array($v1) && strtolower($v1)=='all') {
                $tmp[] = '<div>'.$tmp_stat.$v1.'</div>';
                continue;
            }
            foreach($v1 as $k2=>$v2) {
                if (!is_array($v2) && strtolower($v2)=='all') {
                    $tmp_section[] = '<div>'.$tmp_stat.'<b>'.$k2.'</b> ('.$v2.')'.'</div>';
                    continue;
                }
                $tmp3 = array();
                foreach($v2 as $k3=>$v3){
                    $tmp3[] = $v3." ({$k3})";
                }
                //$tmp_section[] = (strtoupper($k2)=='ASIA' || strtoupper($k2)=='EUROPE' || strtoupper($k2)=='OCEANIA') ? '<p>'.$tmp_stat.implode(', ',$tmp3).'</p>' : '<p>'.$tmp_stat.'<b>'.$k2.'</b> - '.implode(', ', $tmp3).'</p>';
                $tmp_section[] = (strtoupper($k2)==strtoupper($k1)) ? '<div>'.$tmp_stat.implode(', ',$tmp3).'</div>' : '<div>'.$tmp_stat.'<b>'.$k2.'</b> - '.implode(', ', $tmp3).'</div>';
            }
            $tmp[] = implode('',$tmp_section);
        }
        $region_country_list = implode('',$tmp);
        return $region_country_list;
    }

    //get the country list.
    public function getRegionCountryCondtion($country_arr) {
        $tmp_country_code = array();
        foreach($country_arr as $key=>$value) {
            if (!is_array($value) && strtoupper($value)=='ALL') {
                $tmp_country_code[$key] = self::getCountryList($key);
                continue;
            }
            foreach($value as $sub_key=>$sub_val) {
                $tmp_country_code[$key][$sub_key] = $sub_val;
            }
        }
        return $tmp_country_code;
    }

    //get countries array by region
    public function getCountryCodesListByRegion($country_arr) {
        $result = array();
        foreach($country_arr as $key=>$value) {
            foreach($value as $sub=>$sub_val) {
                foreach($sub_val as $code=>$name) {
                    $result[$key][$code] = $name;//array_keys($sub_val);
                }
            }
        }
        return $result;
    }

    //get the peggin country code.
    public function getPegginCountryCode($country_code) {
        $pegging_countries_code = array();
        if (!empty($country_code)) {
            foreach($country_code as $region=>$region_val) {
                foreach($region_val as $area=>$area_val) {
                    foreach($area_val as $key=>$value) {
                        $pegging_countries_code[$key] = array('Region'=>$region,'Area'=>$area);
                    }
                }
            }
        }
        return $pegging_countries_code;
    }

    //write the temp body when you get report by condition.
    public function writeTempBodyToFile($rand_code,$content) {
        $base_path = self::DEFAULT_BASE_LIB_PATH;
        $file_path = self::EXPORT_TMP_FILE_PATH;
        $file_name = self::EXPORT_TMP_FILE_NAME.$rand_code.'.tmp';
        $real_file_path = $base_path.'/'.$file_path;
        $real_file_name = $real_file_path.'/'.$file_name;
        
        //find the file was last accessed n minutes ago
        $find_command = " find ".$real_file_path." -name '*.tmp' -amin +300 | wc -l ";
        @exec($find_command,$echo,$return);
        if (isset($echo[0]) && $echo[0]>10) {
            $clear_command = " find ".$real_file_path." -name '*.tmp' -amin +300 -exec rm -rf {} \;";
            @exec($clear_command,$echos,$returns);
        }

        if (is_writeable($real_file_path)) {
            $handle = fopen($real_file_name,'w+');
            fwrite($handle,$content);
            fclose($handle);
            return true;
        }
        return false;
    }

    private function getApcData($key) {
        $data = apc_fetch($key);
        return $data;
    }

}


// AC is an alias of AppConfiguration
final class AC extends AppConfiguration{}

?>
